package com.ficus.road.maintain.handler.smartdecision.strategy;

import com.ficus.road.maintain.core.model.smartnew.SmartDecisionExcelVO;
import com.ficus.road.maintain.core.model.smartnew.SmartDecisionResponse;
import com.ficus.road.maintain.core.model.smartnew.SmartTable;
import com.ficus.road.maintain.core.model.smartnew.SmartValuation;
import com.ficus.road.maintain.dao.smartnew.SmartValuationMergedDao;
import com.ficus.road.maintain.dao.smartnew.SmartValuationUnmergedDao;
import com.ficus.road.maintain.handler.smartdecision.enums.SmartDecisionStrategyEnum;
import com.ficus.road.maintain.util.FormatUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * @author xiawen yang
 * @date 2022/7/21 下午12:00
 */
@Component
@RequiredArgsConstructor
public class SmartDecisionHighwayValuationStrategy implements ISmartDecisionStrategy {

    private static final double THRESHOLD_PRICE = 0.875;
    private static final int THRESHOLD_PI = 10;
    private final SmartValuationMergedDao smartValuationMergedDao;
    private final SmartValuationUnmergedDao smartValuationUnmergedDao;

    @Override
    public SmartDecisionStrategyEnum getStrategy() {
        return SmartDecisionStrategyEnum.HIGHWAY_VALUATION;
    }

    @Override
    public SmartDecisionResponse process(List<SmartDecisionExcelVO> params, SmartDecisionResponse response) {
        process0(response.getSmartSortingResponse().getTablesBeforeMerge(), false, response);
        process0(response.getSmartSortingResponse().getTablesAfterMerge(), true, response);
        return response;
    }

    public void process0(List<SmartTable> smartTables, boolean isMerged, SmartDecisionResponse response) {
        if (!isMerged) {
            smartTables = smartTables.stream().filter(item -> !item.getType().equals("储备库")).collect(Collectors.toList());
        }
        AtomicReference<Double> moneyBeforeMerge = new AtomicReference<>(0d);
        AtomicReference<Double> moneyAfterMerge = new AtomicReference<>(0d);
        smartTables.forEach(table -> {
            String character = table.getRoadCharacter();
            String level = table.getLevel();
            double pssi;
            if (isMerged) {
                pssi = table.getPSSIMerge();
            } else {
                pssi = table.getPSSI();
            }
            double area = FormatUtil.keepDecimalsOfK(table.getArea(), 2);
            int price = 0;
            if (character.contains("沥青")) {
                if (level.contains("高速")) {
                    if (pssi >= 90 - THRESHOLD_PI) {
                        price = 350;
                    } else {
                        price = 1180;
                    }
                } else if (level.contains("一级")) {
                    if (pssi >= 85 - THRESHOLD_PI) {
                        price = 320;
                    } else {
                        price = 1020;
                    }
                } else if (level.contains("二级")) {
                    if (pssi >= 80 - THRESHOLD_PI) {
                        price = 320;
                    } else {
                        price = 1020;
                    }
                } else {
                    if (pssi >= 75 - THRESHOLD_PI) {
                        price = 320;
                    } else {
                        price = 950;
                    }
                }
            } else if (character.contains("水泥")) {
                price = 720;
            }
            price *= THRESHOLD_PRICE;
            double money = FormatUtil.keepDecimalsOfK((area * price) / 10000, 2);
            SmartValuation smartValuation = new SmartValuation(table, money, price);
            if (isMerged) {
                smartValuationMergedDao.saveOrUpdate(smartValuation);
                moneyAfterMerge.updateAndGet(v -> v + money);
            } else {
                smartValuationUnmergedDao.saveOrUpdate(smartValuation);
                moneyBeforeMerge.updateAndGet(v -> v + money);
            }
        });
        if (isMerged) {
            response.getSmartValuationResponse().setMoneyAfterMerge(FormatUtil.keepDecimalsOfK(moneyAfterMerge.get(), 2));
        } else {
            response.getSmartValuationResponse().setMoneyBeforeMerge(FormatUtil.keepDecimalsOfK(moneyBeforeMerge.get(), 2));
        }
    }
}
